// JavaScript source code

window.onload = function () {
  //点击鼠标左键（e.button==0)时，全部菜单消失
  color=document.getElementById("page1").style.background;
  firstLoadingCookie();
  document.onclick = function () {
    var allmenu = document.getElementsByClassName("r-e");
    for (var r = 0; r <= allmenu.length - 1; r++) {
      allmenu[r].style.display = "none";
    }
  };
  readCookie();
  var keepNow = setInterval(getNowTime, 500);
  var k = setInterval(seth, 500);
  var hide = setInterval(hidess, 500);
  var ininput = setInterval(inputSitaution, 500);
  document.getElementById("add-like").addEventListener("click", creatNewLike);
  //var keyOpen = setInterval(enterKeyManage, 500);
  document.getElementById("enter").addEventListener("click", search);
  // document.getElementById("search").addEventListener('onkeypress', enterKeyManage)
  //绘制下拉栏的尺寸
  /*var w = document.getElementById("search-way").offsetWidth;
    var h = document.getElementById("search-way").offsetHeight;
    document.getElementById("search-way-list").style.width = w + "px";
    document.getElementById("search-way-list").style.height = h + "px";*/
  var m = setInterval(getSuggestion, 500);
  
  //part of NOTE Onload

  document.getElementById("add").addEventListener("click", createNewNote);
  document.getElementById("close").addEventListener("click", function () {
    document.getElementById("write").style.display = "none";
  });
  //createNewNote();

  var keepsave = setInterval(save, 500);
  //var speedSave = setInterval(speedSave, 1);
  document.getElementById("text").setAttribute("onkeypress", "speedSave(event)");
  readNote();
};


function firstLoadingCookie() {
  var c = document.cookie;
  if(c==""){
    document.cookie = "like= ; note= ;expires=Thu, 18 Dec 2043 12:00:00 GMT; path=/";}
  else{
    return;
  }

}

function readCookie() {
  //读取并解析like的cookie
  var like = getCookie("like");
  like = like.split(",");
  like.pop();
  for (var a = 0; a <= like.length - 1; a++) {
    addLike(like[a]);
  }
}
function seth() {
  var l = document.getElementById("search").value;
  if (l == " ") {
    suggest = "";
    document.getElementById("search-suggestion").innerHTML = "";
  }
  alw = document.getElementsByClassName("like")[0].offsetWidth;
  //
  //document.getElementById("add-like").style.height = alw + "px";
  //设置搜索框的holdpalace
  var smode = document.getElementById("search-way-list").value;
  document
    .getElementById("search")
    .setAttribute("placeholder", "在" + smode + "搜索");
  //设置提示栏的宽度；
  var frame = document.getElementById("search-frame").offsetWidth;
  document.getElementById("search-suggestion").style.width = frame * 0.9 + "px";
  //设置喜欢节点的Height
  var like = document.getElementsByClassName("like");
  for (var a = 0; a <= like.length - 1; a++) {
    like[a].style.height = alw + "px";
  }
  //存储like区域的cookie信息
  var da = "";
  var data = document.getElementsByClassName("like");
  if (data.length != 0) {
    var href = document.getElementsByClassName("href");
    for (var b = 0; b <= href.length - 1; b++) {
      var m = href[b].innerHTML;
      da = da + m + ",";
    }
    document.cookie =
      "like=" + da + ";expires=Thu, 18 Dec 2043 12:00:00 GMT; path=/";
  }
  if (data.length == 0) {
    document.cookie = "like=;expires=Thu, 18 Dec 2043 12:00:00 GMT; path=/";
  }
}
function hidess(){
  var sg=document.getElementById("search").value
  if (sg=='') {
    document.getElementById("search-suggestion").innerHTML="";
  }
}
function getNowTime() {
  var time = new Date();
  var h = time.getHours();
  var m = time.getMinutes();
  var s = time.getSeconds();
  document.getElementById("time").innerHTML = h + ":" + m + ":" + s;
}
function changeWaylistPos() {
  var l = document.getElementById("enter").clientLeft;
  var t = document.getElementById("enter").clientTop;
  //console.log(t)
  document.getElementById("way-list").style.marginLeft = l + "px";
  document.getElementById("way-list").style.marginTop = t + "px";
}
function search() {
  //转到搜索引擎网页
  var inf = document.getElementById("search").value;
  var way = document.getElementById("search-way-list").value;
  if (way == "baidu") {
    inf = searchWayList.baidu + inf;
  }
  if (way == "bing") {
    inf = searchWayList.bing + inf;
  }
  window.open(inf);
  document.getElementById("search").value = "";
}
function inputSitaution() {
  //是输入框获取焦点是显示提示栏
  var sita = document.activeElement.id;
  if (sita == "search") {
    document.getElementById("search-suggestion").style.display = "block";
  } else {
    //document.getElementById("search-suggestion").style.display="none";
  }
}
function enterKeyManage(event) {
  var key = event.key;
  if (key == "Enter") {
    search();
  }
}

function creatNewLike() {
  var inputurl = prompt("请输入需要添加网站的URL：", "http://");
  if (inputurl != null && inputurl != "") {
    addLike(inputurl);
  } else {
    alert("无法添加空白的链接。");
    return;
  }
}
function addLike(url) {
  var sign = document.createElement("div");
  var name = document.createElement("div");
  var href = document.createElement("div");

  //name.innerHTML=getURLtitle(url);//获取该url的title
  name.innerHTML = url;
  href.innerHTML = url;
  href.classList.add("href");
  sign.classList.add("sign");
  name.classList.add("name");
  href.style.display = "none";
  like = document.createElement("button");
  var s = name.innerHTML.toString().charAt(0);
  sign.innerHTML = s;

  name.innerHTML = url;
  th = this;
  like.addEventListener("click", function () {
    window.open(url);
  });
  like.onmousedown = function (e) {
    if (e.button == 2) {
      //alert("be click");
      var x = e.clientX;
      var y = e.clientY;
      document.getElementById("like-menu").style.display = "block";
      document.getElementById("like-menu").style.top = y + "px";
      document.getElementById("like-menu").style.left = x + "px";
      likeMenu(event);
    }
  };

  like.appendChild(sign);
  like.appendChild(name);
  like.appendChild(href);
  like.classList.add("like");

  document.getElementById("like-frame").appendChild(like);
}
//创建鼠标右键编辑like函数
window.oncontextmenu = function (e) {
  //取消默认的浏览器自带右键
  e.preventDefault();
  //获取自定义的右键菜单
};
//调用右键菜单
function likeMenu(obj) {
  var t = obj.target;
  if (t.classList.contains("like") == true) {
    document.getElementById("delete").addEventListener("click", function () {
      var object = obj.target; //获取被点击对象
      //console.log(object);
      object.remove();
      document.getElementById("like-menu").style.display = "none";
    });
    document.getElementById("editor").addEventListener("click", function () {
      var object = obj.target;
      var h = object.children[1].innerHTML;
      var res = prompt("将此链接的url修改为：", h);
      if (res != "" && res != null) {
        //object.innerHTML=getURLtitle(res).charAt(0);
        //object.nextSibling.innerHTML=getURLtitle(res);
        object.children[0] = res.charAt(0);
        object.children[1].innerHTML = res;
        object.children[2].innerHTML = res;
      }
      document.getElementById("like-menu").style.display = "none";
    });
  }
}

searchWayList = {
  baidu: "https://www.baidu.com/s?wd=",
  bing: "https://cn.bing.com/search?q=",
};
function getSuggestion() {
  //组装查询地址
  key = document.getElementById("search").value;
  var sugurl =
    "http://suggestion.baidu.com/su?wd=#content#&cb=window.baidu.sug";
  var content = key;
  sugurl = sugurl.replace("#content#", content);
  //定义回调函数
  window.baidu = {
    sug: function (json) {
      suggest = json.s;
      suggest = suggest.toString();
      suggest = suggest.split(",");
      searchSuggestion();
    },
  };
  //动态添加JS脚本
  var script = document.createElement("script");
  script.src = sugurl;
  document.getElementsByTagName("head")[0].appendChild(script);
}

function searchSuggestion() {
  var test = document.getElementById("search-suggestion").children.length;
  var l = document.getElementById("search").value;
  if (test != 0) {
    document.getElementById("search-suggestion").innerHTML = "";
  }
  if (l == " ") {
    document.getElementById("search-suggestion").innerHTML = "";
  }
  if (l != "") {
    if (typeof suggest != null || typeof suggest != "undefined") {
      for (var i = 0; i <= suggest.length - 1; i++) {
        var res = document.createElement("button");
        res.setAttribute("id", "result" + i);
        res.innerHTML = suggest[i];
        res.style.display = "block";
        res.classList.add("search-suggestiong-result");
        res.onclick = function () {
          document.getElementById("search").value = this.innerHTML;
          document.getElementById("enter").click();
        };
        document.getElementById("search-suggestion").appendChild(res);
      }
    }
  } else {
    document.getElementById("search-suggestion").style.height = "0px";
  }
}
function getCookie(inf) {
  var cookie = document.cookie;
  cookie = cookie.split(";");
  for (var n = 0; n <= cookie.length; n++) {
    var m = cookie[n].split("=");
    if (m[0] == inf) {
      return m[1];
    }
  }
}
//获取指定url的title标签
var title;
function get(str) {
  if (window.XMLHttpRequest) {
    xmlhttp = new XMLHttpRequest();
  } else {
    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  }
  xmlhttp.onreadystatechange = function () {
    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
      title = xmlhttp.responseText;
    }
  };
  xmlhttp.open("GET", "server.php?q=" + str, true); //请求php
  xmlhttp.send();
}
function getURLtitle(url) {
  if (url != "") {
    get(url);
    var a = title.indexOf("<title>") + 7;
    var b = title.indexOf("</title>");
    var doc = title.slice(a, b);
    return doc;
  } else {
    return "null";
    throw "url can't be empty.";
  }
}

//note>>>




function speedSave(event) {
  var alt = event.altKey;
  var s = event.key;
  if (s == "Home") {
    document.getElementById("close").click();
  }
}
function createNewNote() {
  note = document.createElement("div");
  note.classList.add("new-note");
  note.style.background = "#F09B59";
  setTimeout("note.style.background='#FFF68E'", 500);
  var text = document.createElement("div");
  var open = document.createElement("button");
  var drop = document.createElement("button");
  text.innerHTML = "新文本"; //
  open.classList.add("normal-btn");
  drop.classList.add("risk-btn");
  text.classList.add("text");
  open.innerHTML = "打开";
  drop.innerHTML = "删除";
  text.setAttribute("style", "overflow:hidden;");
  drop.addEventListener("click", function () {
    console.log(this);
    var sure = confirm("确定永久删除此笔记吗？");
    if (sure == true) {
      this.parentNode.remove();
    }
  });
  open.addEventListener("click", function () {
    var t = this.parentNode.childNodes[0].innerHTML;
    t1 = this.parentNode.childNodes[0];
    t = t.split("↙").join("\n"); //处理textarea换行问题
    document.getElementById("text").value = t;
    document.getElementById("write").style.display = "block";
    document.getElementById("close").addEventListener("click", function () {
      var cr = document.getElementById("text").value;
      cr = cr.split("\n").join("↙"); //处理textarea换行问题
      t1.innerHTML = cr;
    });
  });
  note.appendChild(text);
  note.appendChild(open);
  note.appendChild(drop);
  //document.getElementById("list").appendChild(note);
  var all = document.querySelector(".new-note");
  if (all == " ") {
    document.getElementById("list").appendChild(note);
  } else {
    document.getElementById("list").insertBefore(note, all);
  }
}

//载入重新构建全部节点
function designNewNote(info) {
  note = document.createElement("div");
  note.classList.add("new-note");
  //添加不改变颜色
  //note.style.background="#F09B59";
  //setTimeout("note.style.background='#FFF68E'",500);
  var text = document.createElement("div");
  var open = document.createElement("button");
  var drop = document.createElement("button");
  text.innerHTML = info; //
  open.classList.add("normal-btn");
  drop.classList.add("risk-btn");
  text.classList.add("text");
  open.innerHTML = "打开";
  drop.innerHTML = "删除";
  text.setAttribute("style", "overflow:hidden;");
  drop.addEventListener("click", function () {
    console.log(this);
    var sure = confirm("确定永久删除此笔记吗？");
    if (sure == true) {
      this.parentNode.remove();
    }
  });
  open.addEventListener("click", function () {
    var t = this.parentNode.childNodes[0].innerHTML;
    t1 = this.parentNode.childNodes[0];
    t = t.split("↙").join("\n"); //处理textarea换行问题
    document.getElementById("text").value = t;
    document.getElementById("write").style.display = "block";
    document.getElementById("close").addEventListener("click", function () {
      var cr = document.getElementById("text").value;
      cr = cr.split("\n").join("↙"); //处理textarea换行问题
      t1.innerHTML = cr;
    });
  });
  note.appendChild(text);
  note.appendChild(open);
  note.appendChild(drop);
  //document.getElementById("list").appendChild(note);
  var all = document.querySelector(".new-note");
  if (all == " ") {
    document.getElementById("list").appendChild(note);
  } else {
    document.getElementById("list").insertBefore(note, all);
  }
}

function dropNote() {}
function getCookie(inf) {
  var cookie = document.cookie;
  cookie = cookie.split(";");
  for (var n = 0; n <= cookie.length; n++) {
    var m = cookie[n].split("=");
    if (m[0] == inf) {
      return m[1];
    }
  }
}
function save() {
  //allnote = document.querySelector(".new-note");
  //allnote=document.getElementById("list").innerHTML;
  //allnote=String(allnote);
  data = "";
  allnote = document.getElementsByClassName("text");
  for (var p = 0; p < allnote.length; p++) {
    data = data + allnote[p].innerHTML + ",";
  }
  document.cookie ="note=" + data + ";expires=Thu, 18 Dec 2043 12:00:00 GMT; path=/";
}
function readNote() {
  notelist = getCookie("note");
  notelist = notelist.split(",");
  for (a = notelist.length - 2; a >= 0; a--) {
    designNewNote(notelist[a]);
  }
  //notelist=JSON.stringify(notelist);
  //notelist=JSON.parse(notelist);

  //document.getElementById("list").innerHTML=notelist.toString();
}
function changePage(){

}
window.onclick=function(e){
  var ob=document.elementFromPoint(e.clientX,e.clientY);
  if(ob.classList.contains("tab")==true){
    clicktab=ob.id;  
    var tab=document.getElementsByClassName("tab")
    for(var a=0;a<=tab.length-1;a++){
    var q=a+1;
    document.getElementById("tab"+q).style.background="rgb(172, 171, 171)";
    document.getElementById("page"+q).style.display="none";
  }
    var t=clicktab;
    var num=t.charAt(3);
    document.getElementById(t).style.background="rgb(0, 88, 240)"
    document.getElementById("page"+num).style.display="block";
  }
}